package com.bupt.liutong.admin.sch.bo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.sch.dao.AlbumDao;
import com.bupt.liutong.admin.sch.dao.AlbumPhotoDao;
import com.bupt.liutong.admin.sch.dto.Album;
import com.bupt.liutong.admin.sch.dto.AlbumPhoto;
import com.bupt.liutong.admin.sch.form.AlbumForm;
import com.bupt.liutong.util.BeanUtils;

public class AlbumBo extends BaseBo {

	private AlbumDao albumDao;
	private AlbumPhotoDao albumPhotoDao;

	public List<AlbumForm> getAlbumsByPage(AlbumForm albumForm,
			HttpServletRequest request) {
		Album album = new Album();
		sm.pagingWithNoCriteria(albumForm, album, "sch_album.getAlbumsCount");
		List<Album> albums = albumDao.getAlbumsByPage(album);
		return BeanUtils.dtoToForm(albums, AlbumForm.class);
	}

	public AlbumForm getAlbumById(AlbumForm albumForm) {
		return getAlbumById(albumForm.getId());
	}

	public AlbumForm getAlbumById(Integer id) {
		AlbumForm resultForm = new AlbumForm();
		Album album = albumDao.getAlbumById(id);
		BeanUtils.copyProperties(resultForm, album);
		return resultForm;
	}

	@Transactional
	public void insertAlbum(AlbumForm albumForm) {
		Album album = new Album();
		BeanUtils.copyProperties(album, albumForm);
		albumDao.insertAlbum(album);
	}

	@Transactional
	public void updateAlbum(AlbumForm albumForm) {
		Album album = new Album();
		BeanUtils.copyProperties(album, albumForm);
		albumDao.updateAlbum(album);
	}

	@Transactional
	public void updateAlbumCover(Integer id, Integer coverId) {
		// get photo info
		AlbumPhoto albumPhoto = albumPhotoDao.getAlbumPhotoById(coverId);
		
		// update album cover url
		Album album = new Album();
		album.setId(id);
		album.setCoverUrl(albumPhoto.getThumbUrl());
		albumDao.updateAlbumCover(album);
	}

	@Transactional
	public void deleteAlbums(String[] ids) {
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				albumDao.deleteAlbum(Integer.parseInt(ids[i]));
			}
		}
	}

	public void setAlbumDao(AlbumDao albumDao) {
		this.albumDao = albumDao;
	}

	public void setAlbumPhotoDao(AlbumPhotoDao albumPhotoDao) {
		this.albumPhotoDao = albumPhotoDao;
	}
}
